package com.test.SocketDemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * ${DESCRIPTION}
 *
 * @author muxiaoyu
 * @create 2017/06/08 9:47
 **/
public class URLConnectionDemo {
    public static void main(String[] args) throws MalformedURLException {
        URLClient urlClient = new URLClient();
        String documentAt = urlClient.getDocumentAt("https://www.baidu.com");
        System.out.println(documentAt);

    }
}

class URLClient {
    protected URLConnection connection;

    public static void main(String[] args) {
    }

    public String getDocumentAt(String urlString) {
        //todo:
        StringBuffer stringBuffer = new StringBuffer();
        try {
            URL url = new URL(urlString);
            connection = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                stringBuffer.append(line + "\n");
            }
            reader.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuffer.toString();
    }
}
